/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.railhunter.ui.component;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;

/**
 *
 * @author Alan
 */
public final class CloseableLabelContainer<E> extends JPanel {
    
    private Collection<E> elements;
    
    private Map<E, CloseableLabel<E>> elementLabelMap = new HashMap<>();
    
    public CloseableLabelContainer(Collection<E> elements) {
        super();
        setElements(elements);
    }
    
    public void addElement(E e) {
        if (elementLabelMap.containsKey(e)) {
            return;
        }
        this.elements.add(e);
        final CloseableLabel<E> closedLabel = new CloseableLabel<>(e);
        closedLabel.addMouseListener(new CloseLabelListener());
        this.add(closedLabel);
        this.validate();
        this.repaint();
        elementLabelMap.put(e, closedLabel);
    }
    
    public void addElements(Collection<E> elements) {
        for (E e : elements) {
            if (!elementLabelMap.containsKey(e)) {
                this.elements.add(e);
                final CloseableLabel<E> closedLabel = new CloseableLabel<>(e);
                closedLabel.addMouseListener(new CloseLabelListener());
                this.add(closedLabel);
            }
        }
        this.validate();
        this.repaint();
    }
    
    public String getElementsString() {
        int index = 0;
        String elementsStr = "";
        for (E e : elements) {
            if (index == 0) {
                elementsStr += e.toString();
            } else {
                elementsStr += "," + e.toString();
            }
            index++;
        }
        return elementsStr;
    }

    public Collection<E> getElements() {
        return elements;
    }

    private void setElements(Collection<E> elements) {
        this.elements = elements;
        for (E e : this.elements) {
            final CloseableLabel<E> closedLabel = new CloseableLabel<>(e);
            closedLabel.addMouseListener(new CloseLabelListener());
            this.add(closedLabel);
            elementLabelMap.put(e, closedLabel);
        }
        this.validate();
        this.repaint();
    }

    public Map<E, CloseableLabel<E>> getElementLabelMap() {
        return elementLabelMap;
    }

    public void setElementLabelMap(Map<E, CloseableLabel<E>> elementLabelMap) {
        this.elementLabelMap = elementLabelMap;
    }
    
    class CloseLabelListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            CloseableLabel<E> closedLabel = (CloseableLabel<E>) e.getSource();
            elements.remove(closedLabel.getElement());
            Container parent = closedLabel.getParent();
            parent.remove(closedLabel);
            parent.validate();
            parent.repaint();
        }
    }
}
